require "proto/noticerank/message/ReqRankServerRewardInfoMessage"
require "proto/noticerank/message/ReqGetRankServerRewardMessage"
require "proto/noticerank/message/ReqRankServerRewardInfoMessage"
require "proto/noticerank/message/ReqRankServerPassListMessage"

BillboardMgr = {}

local mgr = BillboardMgr
mgr.rank_list = {}

local m_pass_list = {} --每关的缓存
local m_pass_rank_list = {} --每个排行榜
local m_red_dot = {}
local m_have_outside = {}

function mgr.GetRankList(type)
    if not mgr.rank_list[type] then
        local config = G.dataTable["t_rankboardreward"]
        for k,v in pairs(config) do
            if mgr.rank_list[v.f_Type] == nil then
                mgr.rank_list[v.f_Type] = {}
            end
            table.insert(mgr.rank_list[v.f_Type], v)
        end
        for _,v in pairs(mgr.rank_list) do
            table.sort(v, function(a,b)
                return a.f_ID < b.f_ID
            end)
        end
    end

    return mgr.rank_list[type]
end

--玩家全服奖励信息
function mgr.req_RankServerRewardInfo(rankType)
    local req = ReqRankServerRewardInfoMessage.New()
    req.type = rankType
    
    sendReqMessage(req)
end

local refreshOutSide = function(type)
    local max = m_pass_rank_list[type].max
    if max > 0 then
        local list = mgr.GetRankList(type)
        local count = 0
        for i=1, #list do
            if list[i].f_Progress <= max then
                count = count + 1
            end
        end
        if count > #m_pass_rank_list[type].rewardList then
            mgr.SetOutSideReward(type, true)
        else
            mgr.SetOutSideReward(type, false)
        end
    end
end

function mgr.res_rankServerRewardInfo(msg)
    m_pass_rank_list[msg.type] = {rewardList = msg.list, passList = msg.passList, max = msg.maxId}

    if not m_red_dot[msg.type] or m_red_dot[msg.type] == 0 then
        for k,v in pairs(msg.passList) do
            if v.isGot == 0 then
                mgr.RefreshRedDot(msg.type, true)
            end
        end
    end

    refreshOutSide(msg.type)
    FireEvent(Config.EventType.Billboard_rank_list)
end


function mgr.GetRankListByType(type)
    return m_pass_rank_list[type]
end

--排行过关数据
function mgr.req_RankServerPassList(type, step)
    if m_pass_list[type] and m_pass_list[type][step] and #m_pass_list[type][step] == 3 then
        return
    end

    local req = ReqRankServerPassListMessage.New()
    req.type = type
    req.step = step
    sendReqMessage(req)
end

function mgr.res_RankServerPassList(msg)
    if not m_pass_list[msg.type] then
        m_pass_list[msg.type] = {}
    end
    m_pass_list[msg.type][msg.step] = msg.passList
    FireEvent(Config.EventType.Billboard_rank_pass_list, msg.passList)
end

function mgr.GetPassRankList(type)
    if not m_pass_list[type] then
        m_pass_list[type] = {}
    end
    return m_pass_list[type]
end

--领取奖励
function mgr.req_GetRankServerReward(type, tempId)
    local req = ReqGetRankServerRewardMessage.New()
    req.type = type
    req.tempId = tempId
    sendReqMessage(req)
end

function mgr.res_GetRankServerReward(msg)
    if msg.state == 0 then
        if #msg.award > 0 then
            logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.award)
            local ls = m_pass_rank_list[msg.type]
            if msg.tempId == 0 then
                local config = G.dataTable["t_rankboardreward"][tostring(msg.tempId)]
                for _,v in pairs(ls.passList) do
                    v.isGot = 1
                end
                mgr.SetOutSideReward(msg.type,false)
                mgr.RefreshRedDot(msg.type, false)
            else
                local config = G.dataTable["t_rankboardreward"][tostring(msg.tempId)]
                local remove_red = true
                for _,v in pairs(ls.passList) do
                    if v.step == config.f_Progress then
                        v.isGot = 1
                        -- break
                        table.insert(m_pass_rank_list[msg.type].rewardList, config.f_ID)
                    else
                        if v.isGot == 0 then
                            remove_red = false
                        end
                    end
                end

                refreshOutSide(msg.type)
                -- #m_pass_rank_list[msg.type].rewardList

                if remove_red and not mgr.HaveOutSideReward(msg.type) then
                    mgr.RefreshRedDot(msg.type, false)
                end
            end
            
            FireEvent(Config.EventType.Billboard_rank_list)
        else
            UISysTips.AddMsg(getLanguage("RaceRankDes11"))
        end
    end
end

function mgr.NeedShowRedDot(type)
    return m_red_dot[type] == 1 or mgr.HaveOutSideReward(type)
end

function mgr.RefreshRedDot(type, show)
    m_red_dot[type] = show and 1 or 0

    FireEvent(Config.EventType.Billboard_rank_reddot_refresh, type)
end

--上线请求，刷新红点
function mgr.OnLoginReq()
    utimer.delayCall(function()
        mgr.req_RankServerRewardInfo(EnumConst.BillRankType.Stage)
    end, 3000)

    utimer.delayCall(function()
        mgr.req_RankServerRewardInfo(EnumConst.BillRankType.Relic)
    end, 4000)

    utimer.delayCall(function()
        mgr.req_RankServerRewardInfo(EnumConst.BillRankType.YongJie)
    end, 5000)
    utimer.delayCall(function()
        mgr.req_RankServerRewardInfo(EnumConst.BillRankType.MagicTravel)
    end, 6000)
end

--有溢出奖励
function mgr.HaveOutSideReward(type)
    return m_have_outside[type]
end

function mgr.SetOutSideReward(type, b)
    m_have_outside[type] = b
end

return BillboardMgr